<!DOCTYPE html PUBLIC "-//W3C//Dtd XHTML 1.0 Transitional//EN" "http://www.w3.org/tr/xhtml1/Dtd/xhtml1-transitional.dtd">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<%@ page import="com.infodms.dms.common.Constant"%>
<%@ page import="java.util.*"%>
<%@taglib uri="/jstl/cout" prefix="c" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<jsp:include page="${contextPath}/common/jsp_head_new.jsp" />
<title>活动方案下发</title>
<% String contextPath = request.getContextPath(); 
   List  executePlans = (List)request.getAttribute("executePlans");
   List  attachList   = (List)request.getAttribute("attachList");
%>
<script type="text/javascript">

</script>
</head>
<body>
<div class="navigation"><img src="<%=contextPath%>/img/nav.gif" />&nbsp;当前位置： 市场活动管理 &gt; 活动方案下发&gt; 活动方案制定</div>
<form method="post" name="fm" id="fm">
<div><img class="nav" src="<%=contextPath%>/img/subNav.gif" />&nbsp;业务范围</div>
	<table class="table_query" align="center">
		<tr class= "tabletitle">
			<th align = "right" width="10%">选择业务范围：</th>
			<th align = "left" >
				<select name="areaId" class="short_sel"  onchange="getDealerAreaId(this.options[this.options.selectedIndex].value);">
					<c:forEach items="${areaList}" var="po">
					<c:choose>
					<c:when test="${po.AREA_ID==compaignMap.AREA_ID}">
						<option value="${po.AREA_ID}" selected="selected">${po.AREA_NAME}</option>
						</c:when>
	          		<c:otherwise><option value="${po.AREA_ID}">${po.AREA_NAME}</option></c:otherwise>
				    </c:choose>
					</c:forEach>
				</select>
				<input type="hidden" name="dealerId" id="dealerId" />
				<input type="hidden" name="area_id" id="area_id" value="" />
				
			</th>
		</tr>
	</table>
	<div><img class="nav" src="<%=contextPath%>/img/subNav.gif" />&nbsp;方案信息</div>
	<table width=100% border="0" align="center" cellpadding="1" cellspacing="1" class="table_query">
		<tr>
			<th colspan="5" align="left">&nbsp;方案录入</th>
		</tr>
		<tr>
			<td class="table_query_2Col_label_6Letter">品牌：</td>
			<td>
				<select name="groupId" id="groupId" class="short_sel" onchange="toClear();">
					<c:forEach items="${list}" var="list">
					<c:choose>
					<c:when test="${list.GROUP_ID==compaignMap.GROUP_ID}">
	                <option value="${list.GROUP_ID}" selected>${list.GROUP_NAME}</option>
	          		</c:when>
	          		<c:otherwise><option value="${list.GROUP_ID}">${list.GROUP_NAME}</option></c:otherwise>
				    </c:choose>
					</c:forEach>
				</select>
			</td>
			<td class="table_query_2Col_label_6Letter">活动车型：</td>
			<td>
				<input type="text" name="campaignModel" id="campaignModel"  value="<c:out value="${compaignMap.MODELCODE}"/>" readonly="readonly" datatype="0,is_null,500" size="30"/>
				<input type="hidden" name="modelId" id="modelId" value="<c:out value="${compaignMap.MODELID}"/>"/>
				<!--<input type="button" name="selectbutton" id="selectbutton" class="mini_btn" value="..." onclick="showMaterialGroupToModel('campaignModel','modelId','true','3','true');"/>
				<input name="button3" type="button" class="mini_btn" onclick="showMaterialGroup_market('groupCode','','true','')" value="..." />
				-->
				<input name="button3" type="button" class="mini_btn" onclick="showMaterialCarType_market('groupCode','','true','')" value="..." />
				<input type="button" name="clearbutton" id="clearbutton" class="cssbutton" value="清除" onClick="toClear('modelId','campaignModel');"/>
				<input type="hidden" name="area_id" id="area_id" value="" />
			</td>
			
		</tr>
		<tr>
			<td class="table_query_2Col_label_6Letter">车厂方案编号：</td>
			<td>
				<input type="text" name="campaignNo" id="campaignNo" value="<c:out value="${compaignMap.CAMPAIGN_NO}"/>" datatype="0,is_digit_letter,30" readonly="readonly"/>
			</td>
			<td class="table_query_2Col_label_6Letter">方案名称：</td>
			<td>
				<input type="text" name="campaignName" id="campaignName" value="<c:out value="${compaignMap.CAMPAIGN_NAME}"/>" datatype="0,is_digit_letter_cn,50" size="30" maxlength="50"/>
			</td>
			
		</tr>
		<tr>
			<td class="table_query_2Col_label_6Letter">活动开始日期：</td>
			<td>
				<input class="short_txt"  type="text" id="startDate" name="startDate" value="<c:out value="${compaignMap.START_DATE}"/>" group="startDate,endDate" datatype="0,is_date,10"/>
				<input class="time_ico" type="button" onClick="showcalendar(event, 'startDate', false);" value="&nbsp;" />
			</td>
			<td class="table_query_2Col_label_6Letter">活动结束日期：</td>
			<td>
				<input class="short_txt"  type="text" id="endDate" name="endDate" value="<c:out value="${compaignMap.END_DATE}"/>" group="startDate,endDate" datatype="0,is_date,10"/>
				<input class="time_ico" type="button" onClick="showcalendar(event, 'endDate', false);" value="&nbsp;" />
			</td>
		</tr>
		<tr>
			<td class="table_query_2Col_label_6Letter">活动主题：</td>
			<td colspan="4">
				<input type="text" name="campaignSubject" id="campaignSubject" value="<c:out value="${compaignMap.CAMPAIGN_SUBJECT}"/>" datatype="1,is_digit_letter_cn,50" size="90" maxlength="50"/>
			</td>
		</tr>
		<tr>
			<td class="table_query_2Col_label_6Letter">活动对象：</td>
			<td colspan="4">
				<input type="text" name="campaignObject" id="campaignObject" value="<c:out value="${compaignMap.CAMPAIGN_OBJECT}"/>" size="90" maxlength="50"/>
			</td>
		</tr>
		<tr>
			<td class="table_query_2Col_label_6Letter">活动目的：</td>
			<td colspan="4">
				<textarea name="campaignPurpose" id="campaignPurpose"  rows="4" cols="70"><c:out value="${compaignMap.CAMPAIGN_PURPOSE}"/></textarea>
			</td>
		</tr>
		<tr>
			<td class="table_query_2Col_label_6Letter">活动要求：</td>
			<td colspan="4">
				<textarea name="campaignNeed" id="campaignNeed" rows="4" cols="70"><c:out value="${compaignMap.CAMPAIGN_NEED}"/></textarea>
			</td>
		</tr>
		<tr>
			<td class="table_query_2Col_label_6Letter">活动主要内容：</td>
			<td colspan="4">
				<textarea name="campaignDesc" id="campaignDesc" rows="4" cols="70"><c:out value="${compaignMap.CAMPAIGN_DESC}"/></textarea>
			</td>
		</tr>
	</table>
	<div><img class="nav" src="<%=contextPath%>/img/subNav.gif" />&nbsp;方案范围</div>
	<table width=100% border="0" align="center" cellpadding="1" cellspacing="1" class="table_list">
		<tr>
			<th colspan="5" align="left">活动方案下发范围
				<input type="hidden"  name="orgId" size="15" value=""  id="orgId" class="middle_txt" datatype="1,is_noquotation,500" />
			<input type="hidden"  name="orgCode" size="15" value=""  id="orgCode" class="middle_txt" datatype="1,is_noquotation,500" />
		<input name="orgbu"  id="orgbu" type="button" class="cssbutton" onclick="showAllOrgs('orgCode','orgId','true','',document.getElementById('areaId').value)" value="新增" />
			</th>
		</tr>
		<tr>
		<th>区域编码</th>
		<th>区域</th>
		<th>操作</th>
		</tr>
		<tbody id="dealerTable">
		<% if(executePlans!=null&&executePlans.size()!=0){ %>
    	<c:forEach items="${executePlans}" var="epList">
       		<tr class="table_list_row" id="<c:out value="${epList.ORG_ID}"/>">
            	<td><c:out value="${epList.ORG_CODE}"/></td>
            	<td><c:out value="${epList.ORG_NAME}"/><input type="hidden" name="dlrIds" value="${epList.ORG_ID}"></input></td>
	            <td><input type='button' class='normal_btn' onclick='delRow(<c:out value="${epList.ORG_ID}"/>)' value='删除'/></td>
           	 	<td id="1"></td>
        	</tr>
    	</c:forEach>
       <%} %>
    	</tbody>
	</table>
	<div><img class="nav" src="<%=contextPath%>/img/subNav.gif" />&nbsp;方案附件</div>
	<table width=100% border="0" align="center" cellpadding="1" cellspacing="1" class="table_info">
		<tr>
	        <th colspan="3" align="left">附件列表：<input type="hidden" id="fjids" name="fjids"/>
				<span>
					<input type="button" class="cssbutton"  onclick="showUpload('<%=contextPath%>')" value ='添加附件'/>
				</span>
			</th>
		</tr>
		
		<tr>
			<td width="100%" colspan="2"><jsp:include page="${contextPath}/uploadDiv.jsp" /></td>
  		</tr>
  		<table id="attachTab" class="table_info">
  		<% if(attachList!=null&&attachList.size()!=0){ %>
  		<c:forEach items="${attachList}" var="attls">
		    <tr class="table_list_row1" id="${attls.FJID}">
		    <td><a target="_blank" href="${attls.FILEURL}">${attls.FILENAME}</a></td>
		    <td><input type=button onclick="delAttach('${attls.FJID}')" class="normal_btn" value="删 除"/></td>
		    </tr>
		</c:forEach>
		<%} %>
		</table>
	</table>
	<table width=100% border="0" align="center" cellpadding="1" cellspacing="1" class="table_query">
		<tr>
			<td colspan="4" align="center">
				<input type="hidden" name="campaignId" id="campaignId" value="${campaignId}"/>
				<input type="hidden" name="delDlrIds" id="delDlrIds" value=""/>
				<input type="hidden" name="delAttachs" id="delAttachs" value=""/>
				<input type="hidden" name="dealerIds" id="dealerIds" value=""/>
				<input type="button" class="cssbutton" name="saveBtn" onClick="saveModify();" value="保存"/>
				<input type="button" class="cssbutton" name="assignBtn" onClick="toAssign();" value="下发"/>
				<input type="button" class="cssbutton" name="backBtn" onClick="history.back();" value="返回"/>
			</td>
		</tr>
	</table>
</form>
<script type="text/javascript">
//设置业务范围ID,经销商ID
function getDealerAreaId(arg){
	var areaObj = document.getElementById("areaId");
	var areaId = areaObj.value.split("|")[0];
	//var dealerId = areaObj.value.split("|")[1];
	//document.getElementById("dealerId").value = dealerId;
	//getAvailableAmount();
	var area_id = arg.split("|")[0];
	document.getElementById("area_id").value=area_id;
	 var tbodyNode=document.getElementById("dealerTable");
	    var trNode=document.getElementById(dealerId);
	    var flag = trNode.lastChild.id;
	    var delDlrIds = document.getElementById("delDlrIds").value;
	    
	    if(flag.indexOf("1")==0){
	    	delDlrIds = delDlrIds+","+dealerId;
	    	document.getElementById("delDlrIds").value = delDlrIds;
	        
	    }
		tbodyNode.removeChild(trNode);
}
    //初始化
    function doInit(){
   		loadcalendar();  //初始化时间控件
   		getDealerAreaId(document.getElementById("areaId").value);
	}
	//选择活动车型
	function toSelect(){
		OpenHtmlWindow('<%=contextPath%>/sales/marketmanage/planissued/ActivitiesPlanMake/queryModelInit.do',800,450);
	}
	
	//保存更新后的信息
	function saveModify(){
		if(!isAfterNowTime(document.getElementById('startDate').value)) {
			MyAlert("活动日期不能晚于当前日期！") ;

			return false ;
		}
		var length = document.getElementById("dealerTable").rows.length;
	    if(length==0){
	    	MyAlert("请选择活动经销商范围");
	    	return;
	    }else{
	      if(submitForm('fm')){
	    	MyConfirm("是否确认保存？", confirmUpdate);
	       }
	    }
	}
	
	//确认更新
	function confirmUpdate(){
	   
		makeNomalFormCall('<%=contextPath%>/sales/marketmanage/planissued/ActivitiesPlanMake/updateExecutePlan.json',showResult,'fm');
	}
	
	
	//回调函数
	function showResult(json){
		if(json.returnValue == '1'){
			window.parent.MyAlert("操作成功！");
			fm.action='<%=contextPath%>/sales/marketmanage/planissued/ActivitiesPlanMake/activitiesPlanMakeInit.do';
			fm.submit();
		}else{
			MyAlert("操作失败！请联系系统管理员！");
		}
	}
	
	
	//清除功能
	function toClear(value1,value2){
		document.getElementById(value1).value="";
		document.getElementById(value2).value="";
	}
	
	
	//选择经销商
	function toAddDealer(){
		var campaignId = document.getElementById("campaignId").value;
		OpenHtmlWindow('<%=contextPath%>/sales/marketmanage/planissued/ActivitiesPlanMake/queryDealerInit.do?&campaignId='+campaignId,800,450);
	}
	
	
	//接收子窗口传回的值
	function getDealerInfo(dealerIds,str){
	 	var ids = document.getElementById("dealerIds").value = str;
	 	var arrIDs = ids.split(',');
	 	var arrInfo = dealerIds.split('@,');
	 	for(var i = 0;i<arrIDs.length;i++){
	 		var arr = arrInfo[i].split(",");
	 		var code = arr[0];
	 		var name = arr[1];
	 		var org = arr[2];
	 		var trNode = document.getElementById(arrIDs[i]);
	 		if(trNode!=null){
	 			var trID = trNode.id;
	 			if(trID.indexOf(arrIDs[i])==0){
            		alert("该区域已存在，请选择其他区域！");
		    	}else{
		    		addRow(arrIDs[i],name,code,org,prn);
		    	}
	 		}else{
	 			addRow(arrIDs[i],name,code);
	 		}
	 	}
	 	return;
	 	
	}
	
	//根据取到的值动态生成表格
	function addRow(orgid,orgName,orgcode){
	    var addTable = document.getElementById("dealerTable");
		var rows = addTable.rows;
		var length = rows.length;
		var insertRow = addTable.insertRow(length);
		insertRow.className = "table_list_row";
		insertRow.id = orgid;
		insertRow.insertCell(0);
		insertRow.insertCell(1);
		insertRow.insertCell(2);
		addTable.rows[length].cells[0].innerHTML =  "<td nowrap='nowrap'>"+orgcode+"</td>";
		addTable.rows[length].cells[1].innerHTML =  "<td nowrap='nowrap'>"+orgName+"<input type='hidden' name='dlrIds' id='"+orgid+"' value='"+orgid+"'/></td>";
		addTable.rows[length].cells[2].innerHTML =  "<td nowrap='nowrap'><input type='button' class='normal_btn' onclick='delRow("+orgid+")' value='删除'/></td>";
	}
	
	
	// 删除表格
	function delRow(dealerId){
	    var tbodyNode=document.getElementById("dealerTable");
	    var trNode=document.getElementById(dealerId);
	    var flag = trNode.lastChild.id;
	    var delDlrIds = document.getElementById("delDlrIds").value;
	    
	    if(flag.indexOf("1")==0){
	    	delDlrIds = delDlrIds+","+dealerId;
	    	document.getElementById("delDlrIds").value = delDlrIds;
	        
	    }
		tbodyNode.removeChild(trNode);
		
    }
    //删除表
    function delAllRows(){
    	 var tbodyNode=document.getElementById("dealerTable");
        var rowscount=tbodyNode.rows.length;
    	for(i=rowscount-1;i >= 0; i--){
        	tbodyNode.deleteRow(i);
    		  }
        }
	
	
	//删除附件
	function delAttach(value){
  		var fjId = value;
  		var delAttachs = document.getElementById("delAttachs").value;
		document.getElementById(value).style.display="none";
		delAttachs=delAttachs+","+fjId;
		document.getElementById("delAttachs").value=delAttachs;
	}
	
	
	//下发
	function toAssign(){
		if(!isAfterNowTime(document.getElementById('startDate').value)) {
			MyAlert("活动日期不能晚于当前日期！") ;

			return false ;
		}
		
		var length = document.getElementById("dealerTable").rows.length;
	    if(length==0){
	    	MyAlert("请选择活动经销商范围");
	    	return;
	    }else{
			if(submitForm('fm')){
				MyConfirm("是否确认下发？", confirmAssign);
			}
		}
	}
	//清除功能
	function toClear(){
		document.getElementById("campaignModel").value="";
		document.getElementById("modelId").value="";
	}
	//确认下发
	function confirmAssign(){
		makeNomalFormCall('<%=contextPath%>/sales/marketmanage/planissued/ActivitiesPlanMake/assignExecutePlan.json',showResult,'fm');
	}

	function isAfterNowTime(value) {
		var aDates = value.split("-") ;
		var times = new Date() ;
	    var year = 0;   
	    var month = 0;   
	    var day = 0; 
	          
	    year  = times.getFullYear();   
	    month  = times.getMonth()+1;   
	    day  = times.getDate();       

	    if (aDates[0] < year) {
			return false ;
	    } else if (aDates[0] == year){
	    	if (aDates[1] < month) {
	    		return false ;
	        } else if (aDates[1] == month){
	        	if (aDates[2] < day) {
	        		return false ;
	            }
	        }
	    }
	    
	    return true ;
	}
</script>
</body>
</html>